package com.bgs.engineering.api.syslog.controller;


import com.bgs.engineering.api.syslog.service.SysLogExceptionService;
import com.bgs.engineering.api.syslog.service.SysLogService;
import com.bgs.engineering.util.FileToOssUtil;
import com.bgs.engineering.util.pageUtil.PageRequest;
import com.bgs.engineering.util.pageUtil.PageResult;
import com.bgs.engineering.api.syslog.model.SysLogExceptionModel;
import com.bgs.engineering.api.syslog.model.SysLogModel;
import com.bgs.engineering.common.controller.BaseController;
import com.bgs.engineering.common.errorCode.CommonException;
import com.bgs.engineering.common.response.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 日志操作服务
 *
 * @auther zcy
 * @data 2023/3/22 13:35
 */
@RestController
@RequestMapping("/syslog")
@Api(tags = "日志操作模块")
public class SysLogController extends BaseController {

  @Autowired
  SysLogService sysLogService;

  @Autowired
  SysLogExceptionService sysLogExceptionService;

  /**
   * 保存操作日志
   *
   * @param sysLogModel
   * @return
   * @throws CommonException
   */
  @PostMapping("/saveSyslog")
  @ApiOperation(value = "用于其他服务的日志调度")
  public CommonResult saveSyslog(SysLogModel sysLogModel) throws CommonException {
    return sysLogService.saveSyslog(sysLogModel);
  }

  /**
   * 分页查询日志
   *
   * @return
   */
  @GetMapping("/selectSysLogByPage")
  @ApiOperation(value = "分页查询日志")
  public CommonResult selectSysLogByPage(PageRequest pageRequest, SysLogModel sysLogModel) {
    return CommonResult.ok(sysLogService.selectSysLogByPage(pageRequest, sysLogModel));
  }

  /**
   * 删除日志
   *
   * @param id
   * @return
   */
  @DeleteMapping("/deleteSysLogById")
  @ApiOperation(value = "删除日志")
  public CommonResult deleteSysLogById(@ApiParam("主键id") Integer id) throws CommonException {
    return sysLogService.deleteSysLogById(id);
  }

  /**
   * 根据id查询日志详情
   *
   * @param id
   * @return
   */
  @GetMapping("/selectSysLogById")
  @ApiOperation(value = "id查询日志")
  public CommonResult selectSysLogById(Integer id) throws CommonException {
    return CommonResult.ok(sysLogService.selectSysLogById(id));
  }


  /**
   * 分页查询异常日志
   *
   * @return
   */
  @GetMapping("/selectSysLogExByPage")
  @ApiOperation(value = "分页查询异常日志")
  public CommonResult selectSysLogExByPage(PageRequest pageRequest, SysLogExceptionModel sysLogExceptionModel) {
    PageResult pageResult = sysLogExceptionService.selectSysLogExByPage(pageRequest, sysLogExceptionModel);
    return CommonResult.ok(pageResult);
  }

  /**
   * 异常日志删除
   *
   * @param id
   * @return
   */
  @DeleteMapping("/deleteSysLogEXById")
  @ApiOperation(value = "删除异常日志")
  public CommonResult deleteSysLogEXById(Integer id) throws CommonException {
    return sysLogExceptionService.deleteSysLogEXById(id);
  }

  /**
   * 日志详情
   *
   * @param id
   * @return
   */
  @GetMapping("/selectSysLogExById")
  @ApiOperation(value = "id查询日志详情")
  public CommonResult selectSysLogExById(Integer id) throws CommonException {
    return CommonResult.ok(sysLogExceptionService.selectSysLogExById(id));
  }

  /**
   * 保存异常日志
   *
   * @param sysLogExceptionModel
   * @return
   * @throws CommonException
   */
  @PostMapping("/saveSysLogEx")
  @ApiOperation(value = "用于其他服务的异常日志调度")
  public CommonResult saveSysLogEx(SysLogExceptionModel sysLogExceptionModel) throws CommonException {
    return sysLogExceptionService.saveSysLogEx(sysLogExceptionModel);
  }


  /**
   * 获取操作日志和异常日志比率
   *
   * @param startTime
   * @param endTime
   * @return
   */
  @GetMapping("/getLogRatio")
  @ApiOperation(value = "获取操作日志和异常日志比率")
  public CommonResult getLogRatio(@ApiParam("开始时间") String startTime, @ApiParam("结束时间") String endTime) throws CommonException {
    HashMap<Object, Object> map = new HashMap<>();
    Integer integer = sysLogService.selectSysLogByTime(startTime, endTime);
    Integer integer1 = sysLogExceptionService.selectSysLogByTime(startTime, endTime);
    map.put("normalLog", integer);
    map.put("abnormalLog", integer1);
    return CommonResult.ok(map);
  }


  /**
   * 获取时间内所有用户操作的次数
   *
   * @param startTime
   * @param endTime
   * @return
   */
  @GetMapping("/getLogAnalysis")
  @ApiOperation(value = "获取时间内所有用户操作的次数")
  public CommonResult getLogAnalysis(@ApiParam("开始时间") String startTime, @ApiParam("结束时间") String endTime) throws CommonException {
    List<Map<String, Object>> logAnalysis = sysLogService.getLogAnalysis(startTime, endTime);
    return CommonResult.ok(logAnalysis);
  }


  /**
   * 获取时间内所有用户操作的次数
   *
   * @param startTime
   * @param endTime
   * @return
   */
  @GetMapping("/getLoginIpCount")
  @ApiOperation(value = "获取时间内所有用户不同ip的登录次数")
  public CommonResult getLoginIpCount(String startTime, String endTime) throws CommonException {
    List<Map<String, Object>> logAnalysis = sysLogService.getLoginIpCount(startTime, endTime);
    return CommonResult.ok(logAnalysis);
  }

}
